package com.hdjd.controller;

import com.hdjd.entities.OrderDetail;
import com.hdjd.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄徵岚
 * @create 2021/1/29 8:46
 */
@RequestMapping("/sureOrder")
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ResponseBody
    @RequestMapping(value = "/cancelOrder",method = RequestMethod.DELETE)
    public Map<String,Object> cancelOrder(Integer ordId){
        Map<String,Object> map = new HashMap<>();
        int flag;
        try{
            flag = orderService.cancelOrder(ordId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "取消订单失败");
            return map;
        }
        if (flag == 0){
            map.put("code", 1002);
            map.put("msg", "取消订单失败");
            return map;
        }else {
            map.put("code", 1001);
            map.put("msg", "取消订单成功");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/submitOrder",method = RequestMethod.GET)
    public Map<String,Object> submitOrder(Integer ordId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> orderTotalPrice;
        try{
            orderTotalPrice = this.orderService.orderTotalPrice(ordId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "获取订单总价失败");
            return map;
        }
        if (orderTotalPrice != null){
            map.put("code", 1001);
            map.put("msg", "获取订单总价成功");
            map.put("orderTotalPrice",orderTotalPrice);
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "获取订单总价失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/payOrder",method = RequestMethod.PUT)
    public Map<String,Object> payOrder(Integer ordId, Integer accId, String accPwd){
        Map<String,Object> orderTotalPrice = this.orderService.orderTotalPrice(ordId);
        double totalPrice = (double)orderTotalPrice.get("ordTotalPrice")+(double)orderTotalPrice.get("shipTotalPrice");
        Map<String,Object> map = new HashMap<>();
        try{
             this.orderService.payOrder(ordId, accId, accPwd, totalPrice);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "支付订单失败");
            return map;
        }
            map.put("code", 1001);
            map.put("msg", "支付订单成功");
            return map;
    }

    @ResponseBody
    @RequestMapping(value = "/editOrderGoNum",method = RequestMethod.PUT)
    public Map<String,Object> editOrderGoNum(Integer detId,Integer goNum){
        Map<String,Object> map = new HashMap<>();
        try{
            this.orderService.editOrderGoNum(detId, goNum);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "订单商品购买数修改失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "订单商品购买数修改成功");
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/selectMyOrder",method = RequestMethod.GET)
    public Map<String,Object> selectMyOrder(Integer accId,Integer ordStatus,Integer pageCurrent, Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> orderDetailAndGoods = null;
        int orderNum = 0;
        try{
            orderDetailAndGoods = this.orderService.selectMyOrder(accId, ordStatus, pageCurrent, pageSize);
            orderNum = this.orderService.selectMyOrderNum(accId, ordStatus);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "订单信息查询失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "订单信息查询成功");
        map.put("orderDetailAndGoods",orderDetailAndGoods);
        map.put("orderNum",orderNum);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/returnMyGoods",method = RequestMethod.PUT)
    public Map<String,Object> returnMyGoods(Integer detId){
        Map<String,Object> map = new HashMap<>();
        int updateAfter;
        try{
            updateAfter = this.orderService.returnMyGoods(detId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "商品退货失败");
            return map;
        }
        if (updateAfter != 0){
            map.put("code", 1001);
            map.put("msg", "商品退货成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "商品退货失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/selectOrderDetail",method = RequestMethod.GET)
    public Map<String,Object> selectOrderDetail(Integer ordId){
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> orderDetailAndGoods = null;
        try{
            orderDetailAndGoods = this.orderService.selectOrderDetailByOrdId(ordId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "订单信息查询失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "订单信息查询成功");
        map.put("orderDetailAndGoods",orderDetailAndGoods);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateOrderReAddress",method = RequestMethod.PUT)
    public Map<String,Object> updateOrderReAddress(Integer ordId, Integer reAddressId){
        Map<String,Object> map = new HashMap<>();
        try{
            this.orderService.updateOrderByOrder(ordId, reAddressId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "订单收获地址修改失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "订单收获地址修改成功");
        return map;
    }
}
